package com.changgou.oauth.controller;

import com.changgou.entity.Result;
import com.changgou.entity.StatusCode;
import com.changgou.oauth.service.AuthService;
import com.changgou.oauth.util.AuthToken;
import com.changgou.oauth.util.CookieUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletResponse;

@Controller
@RequestMapping("/oauth")
public class AuthController {

    @Autowired
    private AuthService authService;

    @Value("${auth.clientId}")
    private String clientId;

    @Value("${auth.clientSecret}")
    private String clientSecret;

    @Value("${auth.cookieDomain}")
    private String cookieDomain;

    @Value("${auth.cookieMaxAge}")
    private int cookieMaxAge;


    /**
     * 测试登录
     * @param username
     * @param password
     * @return
     */
    @PostMapping("/interface/login")
    @ResponseBody
    public Result interfaceLogin( @RequestParam("username") String username, @RequestParam("password") String password){
        if(StringUtils.isEmpty(username)){
            throw new RuntimeException("用户名为空！");
        }

        if(StringUtils.isEmpty(password)){
            throw new RuntimeException("密码为空！");
        }

        try {
            AuthToken authToken = authService.applyToken(clientId, clientSecret, username, password);
            //保存jti到浏览器的cookie里

            saveJtiToCookie(authToken.getJti());

            return new Result(true, StatusCode.OK, "登录成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.LOGINERROR, "用户登录失败");
        }


    }

    private void saveJtiToCookie(String jti){
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletResponse response = requestAttributes.getResponse();
        CookieUtil.addCookie(response, cookieDomain, "/", "uid" , jti, cookieMaxAge , true);
    }

}
